var app = getApp();
var constract = require('../../../utils/constract.js');
var pageNum = 1, scrollY; //分页

Page({

  data: {
    height: '',
    showBottom: true,
  },
  onLoad: function(options) {
    wx.getSystemInfo({
      success: (res) => {
        // console.log(res.windowHeight);
        this.setData({
          height: res.windowHeight
        })
      }
    })
  },
  onShow: function () {

    var that=this;
    var news = [];
    var endScroll = scrollY;

    app.Verification(function (token) {
      
      function getNews(num){
        // debugger;
        wx.request({
          url: constract.sysMessage,
          data: {pageNum: num},
          header: { token: token },
          success: function (res) { 
            news = news.concat(res.data.data.list);      
            // console.log(news);   

            that.setData({
              news: news,
              scrollY: endScroll || 0,
            })
             
            if(num < pageNum) {
              getNews(++num);
            }
          }
        });
        
      };
      getNews(1);

    })
    
  },
  watchScroll: (e)=>{
    // console.log(e);
    scrollY = e.detail.scrollTop;
    console.log(scrollY);
  },
  delete: function (e) { //传入noticeId
    var that = this;
    // var id = e.target.dataset.id;
    var noticeId = e.target.dataset.noticeid;
    var index = e.target.dataset.index;

    wx.showModal({
      title: '提示',
      content: '确定要删除这条消息么？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: constract.deleteMsg,
            data:{id:noticeId},
            success:function(res){
              
              // console.log(res);

              if(+res.data.code === 12000) {
                var news = that.data.news;
                news.splice(index,1);
                // console.log(news);
                that.setData({
                  news: news
                })
              }
              
              
              
              
              //删除后的处理
              // app.Verification(function (token) {
              //   wx.request({
              //     url: constract.sysMessage,
              //     data: {pageNum: pageNum},
              //     header: { token: token },
              //     success: function (res) {
              //       var news = res.data.data;
              //       that.setData({
              //         news: news
              //       });
              //     }
              //   });
              // })
            }
          })
          
        }
      }
    })
  },
  edit:function(e){ //跳转详情
    var id = e.currentTarget.dataset.id;
    var noticeId = e.currentTarget.dataset.noticeid;
    var uid = e.currentTarget.dataset.uid;
    if(uid != null) {
      wx.request({
        url: constract.noticEedit,
        data: { id: noticeId },
        success: function (res) {
          wx.navigateTo({
            url: '/pages/partner/partner?id='+id+'&uid='+uid
          })
        }
      })
    }
  },
  touchstart: function (e) {
    // console.log(e);
    var list = this.data.news;
    var index = e.currentTarget.dataset.index;
    if (e.touches.length == 1) {
      for (var i = 0; i < list.length; i++) {
        if (i != index) {
          // console.log(i);
          list[i].txtStyle = "left:0px";
        }
      };
      this.setData({
        startX: e.touches[0].clientX,
        news:list
      })
    }
  },
  touchmove: function (e) {
    var that = this;
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = that.data.startX - moveX;

      var txtStyle = '';
      if (disX <= 20) {
        txtStyle = "left:0px";
      } else if (disX > 20 && disX < 50) {
        txtStyle = "left:-" + disX + "px";
      } else {
        txtStyle = "left:-120rpx";
      }
      var index = e.currentTarget.dataset.index;
      var list = this.data.news;
      list[index].txtStyle = txtStyle;
      this.setData({
        news: list
      })
    };
  },
  touchend: function (e) {
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX;
      var txtStyle = disX > 50 ? "left:-120rpx" : "left:0px";
      var index = e.currentTarget.dataset.index;
      var list = this.data.news;
      list[index].txtStyle = txtStyle;
      this.setData({
        news: list
      })
    }
  },
  loadMore: function(e){
    // console.log(e);
    // console.log(news);
    var that = this;
    var news = this.data.news;
    //再次加载
    app.Verification(function (token) {
      wx.request({
        url: constract.sysMessage,
        data: {pageNum: ++pageNum},
        header: { token: token },
        success: function (res) {
          // console.log(res);
          
          if(res.data.data.list.length > 0) {
            wx.showToast({
              title:'加载中',
              icon: "loading",
              mask: true,
              duration: 1000,
              success: ()=>{
                var newNews = res.data.data.list;
                news = news.concat(newNews);
                that.setData({
                  news: news
                });
              }
            })
          } else if(that.data.showBottom) {
            wx.showToast({
              title:'我是有底线的',
              icon: "none",
              mask: true,
              duration: 2000,
              success: ()=>{
                --pageNum;
                that.setData({
                  showBottom: false,  
                })
              }
            })
          }
          
        }
      });
    })
  },
})